﻿using Newtonsoft.Json;
using SubVersionOne.ViewModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SubVersionOne
{
    public static class LocalStorageProvider
    {
        /// <summary>
        /// Deserializes a previously saved object model
        /// </summary>
        /// <returns></returns>
        public static List<SelectableItemViewModel> Load()
        {
            if (File.Exists(LocalStorageFile))
            {
                using (StreamReader r = new StreamReader(LocalStorageFile))
                {
                    try
                    {
                        return JsonConvert.DeserializeObject<List<SelectableItemViewModel>>(r.ReadToEnd());
                    }
                    catch
                    {
                        return new List<SelectableItemViewModel>();
                    }
                }
            }
            EventLogger.Info("File doesn't exist.");
            return new List<SelectableItemViewModel>();
        }
        public static void Save(List<SelectableItemViewModel> viewModel)
        {
            var content = JsonConvert.SerializeObject(viewModel);
            File.WriteAllText(LocalStorageFile, content);
        }



        public static SettingsViewModel LoadSettings()
        {
            if (File.Exists(ConfigurationFile))
            {
                using (StreamReader r = new StreamReader(ConfigurationFile))
                {
                    return JsonConvert.DeserializeObject<SettingsViewModel>(r.ReadToEnd());
                }
            }
            EventLogger.Info("File doesn't exist.");
            return new SettingsViewModel();
        }
        public static void SaveSettings( )
        {
            var content = JsonConvert.SerializeObject(SubVersionOne.ViewModelLocator.SettingsStatic);
            File.WriteAllText(ConfigurationFile, content);
        }

        public static void Clear( )
        {
            if (File.Exists(LocalStorageFile))
            {
                File.Delete(LocalStorageFile);
            }
        }


        public static string LocalStorageFile
        {
            get
            {
                return GetPath(Constants.LocalStorage);
            }
        }
        public static string PasswordStorageFile
        {
            get
            {
                return GetPath(Constants.PasswordFile);
            }

        }
        public static string LogFile
        {
            get
            {
                return GetPath(Constants.LogName);
            }
        }
        public static string ConfigurationFile
        {
            get
            {
                return GetPath(Constants.ConfigFile);
            }
        }



        public static string GetPath(string filename)
        {
            return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),Constants.AppName, filename);
        }
    }
}
